/* 
 * @Author: 21克的爱情
 * @Date: 2020-09-03 12:54:32
 * @Email: raohong07@163.com
 * @LastEditors: 21克的爱情
 * @LastEditTime: 2020-10-05 23:29:44
 * @Description: 个人中心
 */

import 'package:extended_image/extended_image.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:ki5k_video/models/video_list.dart';
import 'package:ki5k_video/utils/encrypt.dart';
import 'package:ki5k_video/utils/sqflite/SQliteHelp.dart';
import 'package:ki5k_video/utils/sqflite/SQliteMessage.dart';
import '../../widget/list_title_widget.dart';

class PersonalPage extends StatefulWidget {
  PersonalPage({Key key}) : super(key: key);

  @override
  _PersonalPageState createState() => _PersonalPageState();
}

class _PersonalPageState extends State<PersonalPage> {
  List<Widget> listView = [];
  List<VideoDetail> listSearchData=[];

  @override
  void initState() { 
    super.initState();

    initData();
  }

  void initData() async {
    List<Map<String, dynamic>> data = await SQliteHelp.instance.searchData(SQliteMessage.tablePlayData);
    setState(() {
      listSearchData = data.map((item){
        return VideoDetail.fromJson(Map<String, dynamic>.from(item));
      }).toList();
      print(listSearchData);
    });

    List<Map<String, dynamic>> titleData = [{
      'title': '观看记录',
      'icon': Icons.query_builder,
      'page': '',
    }, {
      'title': '软件设置',
      'icon': Icons.settings,
      'page': '',
    }, {
      'title': '我的收藏',
      'icon': Icons.local_activity,
      'page': '',
    }, {
      'title': '清除缓存',
      'icon': Icons.cached,
      'page': '',
    }, {
      'title': '关于我们',
      'icon': Icons.help,
      'page': '',
    }];

    listView.add(user);

    titleData.forEach((item){
      listView.add(
          ListTitleCustom(
            '${item["title"]}',
            icon: item["icon"],
            contentPadding: EdgeInsets.only(
                left: 20,
                top: 20,
                bottom: 8
            ),
            titlePadding: EdgeInsets.symmetric(
                horizontal: 10
            ),
            onTap: () async {
              print('${item["title"]}');
              if(item["title"] == '清除缓存'){
                SQliteHelp.instance.cleanData(SQliteMessage.tableSearch);
                SQliteHelp.instance.cleanData(SQliteMessage.tablePlayData);
//                SQliteHelp.instance.deleteTable;
              }
            },
          )
      );
      if(item["title"] == '观看记录'){
        listView.add(
            Container(
                height: 90,
                padding: EdgeInsets.only(
                  top: 5,
                  right: 15,
                  left: 15,
                ),
                child: ListView.builder(
                    scrollDirection: Axis.horizontal,
                    itemCount: listSearchData.length > 10 ? 10 : listSearchData.length,
                    itemBuilder: (BuildContext context, int index){
                      return Container(
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                        child: ExtendedImage.network(
                          '${listSearchData[index].vodPic}',
                          fit: BoxFit.cover,
                          shape: BoxShape.rectangle,
                          borderRadius: BorderRadius.all(Radius.circular(10)),
                          clearMemoryCacheWhenDispose: true,
                        ),
                      );
                    }
                )
            )
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        top: MediaQuery.of(context).padding.top,
      ),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [
            Color(0xFF9271e2),
            Theme.of(context).primaryColor,
          ],
          begin: FractionalOffset(1, 1),
          end: FractionalOffset(0, 0),
        ),
      ),
      child: Column(
        children: listView,
      )
    );
  }

  Widget get user{
    return Row(
      children: [
        Container(
          height: 90,
          width: 90,
          margin: EdgeInsets.only(left: 15),
          child: ExtendedImage.asset(
            'assets/images/logo.png',
            fit: BoxFit.cover,
            shape: BoxShape.rectangle,
            borderRadius: BorderRadius.all(Radius.circular(10)),
            clearMemoryCacheWhenDispose: true,
          )
        )
      ],
    );
  }
}